<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Mail{
    private $to;
    private $subject;
    private $var;
    private $temp;
    private $message;
    
    protected function prepareMessage(){
        $file = ProjectConfiguration::getActive()->getRootDir() . '/mailTemplates/' . $this->template;
        if(!file_exists($file)){
            throw new Exception('Nie znaleziono szablonu maila.');
        }
        $pattern = array();
        $value = array();
        foreach($this->var as $name => $value){
            $pattern[] = '/{{' . $name . '}}/';
            $value[] = $value;
        }
        $content = file_get_contents($file);
        $this->message = preg_replace($pattern, $value, $content);
    }
    
    public function setTo($to) {
        $this->to = $to;
    }

    public function setSubject($subject) {
        $this->subject = $subject;
    }

    public function setTemplate($temp) {
        $this->temp = $temp;
    }
    
    public function addToVarHolder($name, $value) {
        $this->var[$name] = $value;
    }
    
    public function send(){
        $this->prepareMessage();
        return mail($this->to, $this->subject, $this->message);
    }

}